﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using GeoFinderClientApp.Model;

namespace GeoFinderClientApp.ViewModel
{
    public class ViewModels
    {

        #region Facebook

        #region Me
        public Friend Me { get; set; }

        public void GetMe()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Count > 0)
            {
                GetSavedMe();
            }
            else
            {
                Me = null;
            }
        }

        public void SaveMe()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            if (settings.Contains("Me"))
            {
                settings["Me"] = Me;
            }
            else
            {
                settings.Add("Me", Me.GetCopy());
            }


            settings.Save();
        }

        public void GetSavedMe()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("Me"))
            {
                Me = (Friend)IsolatedStorageSettings.ApplicationSettings["Me"];
            }

            //MessageBox.Show("Got accomplishments from storage");
        }
        #endregion

        #region AllFriends
        public ObservableCollection<Friend> AllFriends { get; set; }
        
        public void GetFriends()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Count > 0)
            {
                GetSavedFriends();
            }
            else
            {
                GetDefaultFriends();
            }
        }

        public void SaveFriends()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            if (settings.Contains("Friends"))
            {
                settings["Friends"] = AllFriends;
            }
            else
            {
                settings.Add("Friends", AllFriends);
            }

            //foreach (Friend a in AllFriends)
            //{
            //    if (settings.Contains(a.Name))
            //    {
            //        settings[a.Name] = a;
            //    }
            //    else
            //    {
            //        settings.Add(a.Name, a.GetCopy());
            //    }
            //}

            settings.Save();
        }


        public void GetDefaultFriends()
        {
            ObservableCollection<Friend> a = new ObservableCollection<Friend>();

            AllFriends = a;
        }


        public void GetSavedFriends()
        {
            ObservableCollection<Friend> a = new ObservableCollection<Friend>();

           
                try
                {

                    a = (ObservableCollection<Friend>) IsolatedStorageSettings.ApplicationSettings["Friends"];
                }
                catch { }
            
            AllFriends = a;
        }

        #endregion
        #endregion

        #region Settings

        public void getAllOptions()
        {
            GetPushOptions();
            GetTimeOutOptions();
            GetSearchingAreaOptions();
        }

        public void RestoreSavedOptions()
        {
            GetPushOptions();
            GetSearchingAreaOptions();
            GetTimeOutOptions();
        }

        public void SaveAllOptions()
        {
            SavePushOptions();
            SaveSearchingAreaOptions();
            SaveTimeOutOptions();
        }

        #region PushSettings

        public ObservableCollection<PushOption> PushOptions{ get; set; }

        public bool HasSavedPushOptions()
        {
            return IsolatedStorageSettings.ApplicationSettings.Contains(PushType.Push.ToString());
        }

        public void GetPushOptions()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Count > 0)
            {
                GetSavedPushOptions();
                if (PushOptions.Count <= 0)
                {
                    GetDefaultPushOptions();
                }
            }
            else
            {
                GetDefaultPushOptions();
            }
        }

        public void SavePushOptions()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            foreach (PushOption a in PushOptions)
            {
                if (settings.Contains(a.Name.ToString()))
                {
                    settings[a.Name.ToString()] = a;
                }
                else
                {
                    settings.Add(a.Name.ToString(), a.GetCopy());
                }
            }

            settings.Save();
            //MessageBox.Show("Finished saving Options");
        }
        
        public void GetDefaultPushOptions()
        {
            ObservableCollection<PushOption> a = new ObservableCollection<PushOption>();

            a.Add(new PushOption() { Name = PushType.Push, IsEnabled = false });
            a.Add(new PushOption() { Name = PushType.Raw, IsEnabled = true });
            a.Add(new PushOption() { Name = PushType.Tile, IsEnabled = true });
            a.Add(new PushOption() { Name = PushType.Toast, IsEnabled = true });

            PushOptions = a;
            //MessageBox.Show("Got accomplishments from default");
        }
        
        public void GetSavedPushOptions()
        {
            ObservableCollection<PushOption> a = new ObservableCollection<PushOption>();

            foreach (Object o in IsolatedStorageSettings.ApplicationSettings.Values)
            {
                try
                {
                    a.Add((PushOption)o);
                }
                catch { }
            }

            PushOptions = a;
            //MessageBox.Show("Got accomplishments from storage");
        }

        #endregion

        #region TimeOuts

        public ObservableCollection<TimeOutsOption> TimeOutOptions { get; set; }

        public void GetTimeOutOptions()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Count > 0)
            {
                GetSavedTimeOutOptions();
                if (TimeOutOptions.Count <= 0)
                {
                    GetDefaultTimeOutOptions();
                }
            }
            else
            {
                GetDefaultTimeOutOptions();
            }
        }

        public void SaveTimeOutOptions()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            foreach (TimeOutsOption a in TimeOutOptions)
            {
                if (settings.Contains(a.Type.ToString()))
                {
                    settings[a.Type.ToString()] = a;
                }
                else
                {
                    settings.Add(a.Type.ToString(), a.GetCopy());
                }
            }

            settings.Save();
            //MessageBox.Show("Finished saving Options");
        }

        public void GetDefaultTimeOutOptions()
        {
            ObservableCollection<TimeOutsOption> a = new ObservableCollection<TimeOutsOption>();

            a.Add(new TimeOutsOption() { Type = TimeOutType.Facebook, AutomaticRefreshMode = true });
     //       a.Add(new TimeOutsOption() { Type = TimeOutType.Server, AutomaticRefreshMode = true });
     //       a.Add(new TimeOutsOption() { Type = TimeOutType.GPS, AutomaticRefreshMode = true });

            TimeOutOptions = a;
            //MessageBox.Show("Got accomplishments from default");
        }

        public void GetSavedTimeOutOptions()
        {
            ObservableCollection<TimeOutsOption> a = new ObservableCollection<TimeOutsOption>();

            foreach (Object o in IsolatedStorageSettings.ApplicationSettings.Values)
            {
                try
                {
                    a.Add((TimeOutsOption)o);
                }
                catch { }
            }

            TimeOutOptions = a;
            //MessageBox.Show("Got accomplishments from storage");
        }

        #endregion

        #region SearchingAreas

        public ObservableCollection<SearchingAreaOption> SearchingAreaOptions { get; set; }

        public void GetSearchingAreaOptions()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Count > 0)
            {
                GetSavedSearchingAreaOptions();
                if (SearchingAreaOptions.Count <= 0)
                {
                    GetDefaultSearchingAreaOptions();
                }
            }
            else
            {
                GetDefaultSearchingAreaOptions();
            }
        }

        public void SaveSearchingAreaOptions()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            foreach (SearchingAreaOption a in SearchingAreaOptions)
            {
                if (settings.Contains(a.Type.ToString()))
                {
                    settings[a.Type.ToString()] = a;
                }
                else
                {
                    settings.Add(a.Type.ToString(), a.GetCopy());
                }
            }

            settings.Save();
            //MessageBox.Show("Finished saving Options");
        }

        public void GetDefaultSearchingAreaOptions()
        {
            ObservableCollection<SearchingAreaOption> a = new ObservableCollection<SearchingAreaOption>();

            a.Add(new SearchingAreaOption() { Type = SearchingAreaType.All, SearchingArea = 500 });

            SearchingAreaOptions = a;
            //MessageBox.Show("Got accomplishments from default");
        }

        public void GetSavedSearchingAreaOptions()
        {
            ObservableCollection<SearchingAreaOption> a = new ObservableCollection<SearchingAreaOption>();

            foreach (Object o in IsolatedStorageSettings.ApplicationSettings.Values)
            {
                try
                {
                    a.Add((SearchingAreaOption)o);
                }
                catch { }
            }

            SearchingAreaOptions = a;
            //MessageBox.Show("Got accomplishments from storage");
        }

        #endregion
        
        #endregion
    }
}
